/**
 * 部门管理初始化
 */
var Dept = {
    id: "DeptTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Dept.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', align: 'center', valign: 'middle',width:'50px'},
        {title: 'userid', field: 'userid', visible:false , align: 'center', valign: 'middle',width:'50px'},
        {title: '商户简称', field: 'simplename', align: 'center', valign: 'middle', sortable: true},
        {title: '商户全称', field: 'fullname', align: 'center', valign: 'middle', sortable: true},
        {title: '账号', field: 'account', align: 'center', valign: 'middle', sortable: true},
        {title: '开户时间', field: 'create_time', align: 'center', valign: 'middle', sortable: true},
        {title: '网点数量', field: 'dot_num', align: 'center', valign: 'middle', sortable: true},
        {title: '联系人', field: 'contacts', align: 'center', valign: 'middle', sortable: true},
        {title: '联系电话', field: 'contact_phone', align: 'center', valign: 'middle', sortable: true},
        {title: '联系地址', field: 'address', align: 'center', valign: 'middle', sortable: true},
        {title: '短信余额', field: 'message_num', align: 'center', valign: 'middle', sortable: true},
        {title: '电话余额', field: 'call_num', align: 'center', valign: 'middle', sortable: true},
        {title: '状态', field: 'is_use', align: 'center', valign: 'middle', sortable: true,
            formatter : function(value,row,index){
             if(row.is_use=='0'){
                 return "正常";
             }else{
                 return "停用";
             }
        } }
        ];
};

/**
 * 检查是否选中
 */
Dept.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Dept.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加部门
 */
Dept.openAddDept = function () {
    var index = layer.open({
        type: 2,
        shade: 0.01,
        title: '添加商户',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/dept/dept_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看部门详情
 */
Dept.openDeptDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            shade: 0.01,
            title: '商户详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/dept/dept_update/' + Dept.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除部门
 */
Dept.delete = function () {
    if (this.check()) {

        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/dept/delete", function () {
                Feng.success("删除成功!");
                Dept.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("deptId",Dept.seItem.id);
            ajax.start();
        };

        Feng.confirm("是否刪除该商户?", operation);
    }
};

/**
 * 重置密码
 */
Dept.reset = function () {
    if (this.check()) {
        var userId = this.seItem.userid;
        parent.layer.confirm('是否重置密码111111？', {
            btn: ['确定', '取消'],
            shade: false //不显示遮罩
        }, function () {
            var ajax = new $ax(Feng.ctxPath + "/mgr/reset", function (data) {
                Feng.success("重置密码成功!");
            }, function (data) {
                Feng.error("重置密码失败!");
            });
            ajax.set("userId", userId);
            ajax.start();
        });
    }
};

/**
 * 冻结用户账户
 * @param userId
 */
Dept.freezeAccount = function () {
    if (this.check()) {
        var userId = this.seItem.userid;
        var ajax = new $ax(Feng.ctxPath + "/mgr/freeze", function (data) {
            Feng.success("禁用成功!");
            Dept.table.refresh();
        }, function (data) {
            Feng.error("禁用失败!" + data.responseJSON.message + "!");
        });
        ajax.set("userId", userId);
        ajax.start();
    }
};

/**
 * 解除冻结用户账户
 * @param userId
 */
Dept.unfreeze = function () {
    if (this.check()) {
        var userId = this.seItem.userid;
        var ajax = new $ax(Feng.ctxPath + "/mgr/unfreeze", function (data) {
            Feng.success("启用成功!");
            Dept.table.refresh();
        }, function (data) {
            Feng.error("启用失败!");
        });
        ajax.set("userId", userId);
        ajax.start();
    }
}

/**
 * 查询部门列表
 */
Dept.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Dept.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Dept.initColumn();
    var table = new BSTable(Dept.id, "/dept/list", defaultColunms);

    table.init();
    Dept.table = table;
    layui.use('layer', function () {

    })
});
